#!/usr/bin/python
# -*- coding: utf-8 -*-
#Under the terms of the General Public License v.3
#For more information see LICENSE.txt

import thread
from Element import *

class AccessPoint (Element):

	def __init__(self, image, posX, posY, power):
		Element.__init__(self, image, posX, posY)
		self.initConstants()
		self.image.set_colorkey(self.TRANSPARENT_COLOR)
		self.image = pygame.transform.scale(self.image, self.IMAGE_SIZE)
		self.waveRadius = self.MIN_RADIUS
		self.power = power
		self.life = True
		self.initSound()
		self.turnOn()

	def initConstants(self):
		self.TRANSPARENT_COLOR = (0xFF, 0xFF, 0xFF)
		self.IMAGE_SIZE = (40, 40)
		self.MIN_RADIUS = 10
		self.WAVE_COLOR = (0, 0x7F, 0xFF)
		self.WAVE_WIDTH = 1
		self.WAVE_DELAY_MS = 100
		self.SOUND_PATH = "data/sfx/"
		self.SOUND = self.SOUND_PATH + "ap_sound.wav"
		self.SOUND_VOLUME = 0.6

	def initSound(self):
		self.sound = pygame.mixer.Sound(self.SOUND)
		self.sound.set_volume(self.SOUND_VOLUME)

	def isAlive(self):
		return self.life

	def setLife(self, life):
		self.life = life

	def getPower(self):
		return self.power

	def setPower(self, power):
		self.power = power

	def getCenter(self):
		rect = self.image.get_rect()
		return (self.getPosX() + rect.width / 2, self.getPosY() + rect.height / 2)

	def turnOff(self):
		self.setLife(False)

	def turnOn(self):
		self.setLife(True)

	def playSound(self):
		self.sound.play()

	def paint(self, surface):
		thread.start_new_thread(self.paintThread, (surface, 0))

	def paintThread(self, surface, nothing):
		while self.life:
			self.waveRadius %= self.power
			self.waveRadius += self.MIN_RADIUS

			if (self.waveRadius == self.MIN_RADIUS):
				self.playSound()

			Element.paint(self, surface)
			pygame.draw.circle(surface, self.WAVE_COLOR, self.getCenter(), self.waveRadius, self.WAVE_WIDTH)
			pygame.time.delay(self.WAVE_DELAY_MS)
			pygame.display.flip()
